package com.fingard.dsp.bank.directbank.hebbank01;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.util.TransIDHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

/**
 * @author Parker on 2019-10-16.
 * @version 1.0
 */
public class HEBBANK01Base extends DirectBase {
    //银行ID
    protected static final String banks="7023";
    private static TransIDHelper idHelper = new TransIDHelper(1, 999);

    protected String getQryTransID() {
        Date tmpDateNow = new Date();
        return Format.DateTime17Format.format(tmpDateNow) + idHelper.getNextIDStr();
    }

    protected void writeCommonHeader(XmlTextWriter xmlWriter, HEBBANK01Header p_bankHeader) throws Exception {
        if (StringHelper.isNullOrEmpty(p_bankHeader.bsnCode)) {
            throw new Exception("未赋值bsnCode");
        }
        xmlWriter.autoFormat = false;
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"" + getCharset() + "\"?>");
        xmlWriter.writeStartElement("SCCBEBankData");
        xmlWriter.writeStartElement("opReq");
        xmlWriter.writeElementString("bsnCode", p_bankHeader.bsnCode);
        xmlWriter.writeElementString("cstNo", bankFront.org);
        xmlWriter.writeElementString("areaCode", "0000");
        xmlWriter.writeElementString("serialNo", p_bankHeader.serialNo);
        xmlWriter.writeElementString("reqTime", Format.DateTime14Format.format(new Date()));
    }


    protected String getCharset() {
        return getBankConfig().getCharset("UTF-8");
    }

    /**
     * 发送银行前置
     *
     * @param p_reqStr
     * @return
     * @throws Exception
     */
    protected String[] sendToBank(String p_reqStr, HEBBANK01Header p_bankHeader) throws Exception {
        String[] retStr = new String[]{"", ""};
        String tmpUrl = bankFront.serverURL;
        WriteBankLogLn("1.提交银行的地址：" + tmpUrl);

        p_reqStr = p_bankHeader.bsnCode + "|#" + signature(p_reqStr);
        WriteBankLogStep2(p_reqStr);
        WebRequest tmpWebRequest = new WebRequest(tmpUrl);
        tmpWebRequest.setConnectTimeout(60000);
        tmpWebRequest.setRequestProperty("Method", "POST");
        tmpWebRequest.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        tmpWebRequest.setRequestProperty("connection", "close");//设置连接状态
        byte[] tmpUpBytes = p_reqStr.getBytes(getCharset());
        retStr = tmpWebRequest.upload(tmpUpBytes, getCharset());
        WriteBankLogStep3(retStr[1]);
        return retStr;
    }

    //进行签名
    protected String signature(String data) throws Exception {
        String signURL = bankFront.signURL;
        WriteBankLogLn("1.银行签名地址：" + signURL);
        WriteBankLogStep2(data);
        String signedData = "";
        WebRequest tmpWebRequest = new WebRequest(signURL);
        tmpWebRequest.setConnectTimeout(60000);
        tmpWebRequest.setRequestProperty("Content-Length", String.valueOf(data.length()));
        tmpWebRequest.setRequestProperty("Content-Type", "INFOSEC_SIGN/1.0");
        String charsetName = getCharset();
        byte[] tmpToWriteBytes = data.getBytes(charsetName);
        String[] retSign = tmpWebRequest.upload(tmpToWriteBytes,"GBK");

        if (retSign[0].length() == 0) {
            try {
                WriteBankLogLn("3.签名返回的结果：" + retSign[1]);
                XmlTextReader tmpXmlReader = new XmlTextReader(retSign[1]);
                String tmpRest = tmpXmlReader.getSimpleText("result");
                if ("0".equals(tmpRest)) {
                    WriteBankLogLn("签名成功");
                    signedData = tmpXmlReader.getSimpleText("sign");
                } else {
                    WriteBankLogLn("签名失败");
                }
            } catch (Exception e) {
                e.printStackTrace();
                WriteBankLogLn(e.getMessage());
            }
        } else {
            WriteBankLogLn("与银行签名端口通讯失败；详细信息：" + retSign[1]);
        }
        return signedData;
    }

    // 上送格式为json格式
    protected String[] sendToATS(String jsonData) throws Exception {
        String[] retStr = new String[]{"", ""};
        WriteBankLogLn("2.提交ATS的地址：" + FGBiz.dspCenter.dspSet.receiptPushServerUrl);

        // 发送银行前置
        WebRequest tmpRequest = new WebRequest(FGBiz.dspCenter.dspSet.receiptPushServerUrl);
        tmpRequest.setConnectTimeout(10000);
        tmpRequest.setRequestProperty("connection", "Close");
        tmpRequest.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        String charsetName = getCharset();
        StringBuilder data = new StringBuilder();
        data.append(jsonData);
        WriteBankLogLn("3.推送给ATS的数据" + data.toString());
        byte[] tmpWriteBytes = data.toString().getBytes(charsetName);
        retStr = tmpRequest.upload(tmpWriteBytes, charsetName);
        WriteBankLogLn("4.返回的数据" + retStr[1]);

        return retStr;
    }

}
